import { BaseWindow } from "../../../component/window/BaseWindow";
import { ILayer } from "../../../mvc/interfaces/ILayer";
import { DataProvider } from "../../../../fl/data/DataProvider";
import { Language } from "../../../../extend/language/Language";
import { NetDispatcher } from "../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../mvc/ServerCommand";
import { ObjCreate } from "../common/ObjCreate";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { GTextFormat } from "../../../common/GTextFormat";
import { FontUtil } from "../../../common/font/FontUtil";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { ImagesConst } from "../../resource/ImagesConst";
import { TilePanel } from "../equipment/TilePanel";
import { DrugItemCellRenderer } from "./DrugItemCellRenderer";
import { DrugBagCellRenderer } from "./DrugBagCellRenderer";
import { MuiEvent } from "../../../../com/mui/events/MuiEvent";
import { Cache } from "../../cache/Cache";
import { ItemsUtil } from "../shortcuts/item/ItemsUtil";
import { SDrugBagInfo } from "../../../../Message/Game/SDrugBagInfo";
import { EDrug } from "../../../../Message/Public/EDrug";
import { GTabBar } from "../../../../com/mui/controls/GTabBar";
type int = number;
//class DrugBagWindow
    
    export  class DrugBagWindow extends BaseWindow
    {
        constructor(arg1: ILayer=null)
        {
            super(arg1);
            this._drugDP = new DataProvider();
            this._sellDP = new DataProvider();
            this._tabData = Language.getArray(35964);
            this.setSize(265, 355);
            titleHeight = 47;
            this.title = Language.getString(35965);
            NetDispatcher.addCmdListener(ServerCommand.BackpackDataChange, this.serverBagDataChangehandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.serverBagDataChangehandler);
            return;
        }

        protected /* override */ createChildren(): void
        {
            var loc1=null;
            var loc2=0;
            super.createChildren();
            ObjCreate.createBg(17, 60, 230, 115, this);
            this._tabbar = ObjCreate.createGTabBar(16, 33, this._tabData, 65, 22, this, this.onTabBarChangeHandler);
            this._labelTxt = ObjCreate.createTextField(Language.getString(35966), 25, 70, 220, 20, this, GlobalStyle.textFormat6);
            this._labelTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._leftTxt = ObjCreate.createTextField("", 145, 70, 220, 20, this, new GTextFormat(FontUtil.songtiName, 12, 65280));
            this._leftTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._listSprite = new Object /* flash.display.Sprite */();
            this._listSprite.x = 28;
            this._listSprite.y = 90;
            this.addChild(this._listSprite);
            loc2 = 0;
            while (loc2 < 10) 
            {
                loc1 = GlobalClass.getBitmap(ImagesConst.PackItemBg);
                loc1.width = 38;
                loc1.height = 38;
                this._listSprite.addChild(loc1);
                loc1.x = loc2 % 5 * 41;
                loc1.y = Math.floor(loc2 / 5) * 41;
                ++loc2;
            }
            this._drugList = new TilePanel();
            this._drugList.horizontalGap = 3;
            this._drugList.verticalGap = 0;
            this._drugList.columnWidth = 38;
            this._drugList.rowHeight = 41;
            this._drugList.setSize(210, 80);
            this._drugList.move(0, 0);
            this._drugList.setStyle("cellRenderer", DrugItemCellRenderer);
            this._listSprite.addChild(this._drugList);
            this._sellList = new TilePanel();
            this._sellList.horizontalGap = 5;
            this._sellList.verticalGap = 5;
            this._sellList.columnWidth = 230;
            this._sellList.rowHeight = 80;
            this._sellList.setSize(230, 170);
            this._sellList.move(17, 178);
            this._sellList.setStyle("cellRenderer", DrugBagCellRenderer);
            this.addChild(this._sellList);
            return;
        }

        /* internal  */onTabBarChangeHandler(arg1: MuiEvent): void
        {
            var loc1=this._tabbar.selectedIndex;
            this.updateByIndex(loc1);
            this.show();
            return;
        }

        /* internal  */updateByIndex(arg1: int): void
        {
            var loc1=null;
            var loc2=0;
            this._drugDP.removeAll();
            this._sellDP.removeAll();
            if (arg1 != 0) 
            {
                if (arg1 != 1) 
                {
                    if (arg1 == 2) 
                    {
                        this._labelTxt.text = Language.getString(35968);
                        this._sellDP.addItem(13003003);
                        this._sellDP.addItem(13003007);
                        loc1 = Cache.instance.pack.backPackCache.getItemsByJudge(ItemsUtil.isPetChangeshengdan);
                    }
                }
                else 
                {
                    this._labelTxt.text = Language.getString(35967);
                    this._sellDP.addItem(13103000);
                    this._sellDP.addItem(13103101);
                    loc1 = Cache.instance.pack.backPackCache.getItemsByJudge(ItemsUtil.isButianwan);
                }
            }
            else 
            {
                this._labelTxt.text = Language.getString(35966);
                this._sellDP.addItem(13003000);
                this._sellDP.addItem(13003101);
                loc1 = Cache.instance.pack.backPackCache.getItemsByJudge(ItemsUtil.isChangeshengdan);
            }
            if (loc1 && loc1.length > 0) 
            {
                loc2 = 0;
                while (loc2 < loc1.length) 
                {
                    this._drugDP.addItem(loc1[loc2]);
                    ++loc2;
                }
            }
            this._sellList.dataProvider = this._sellDP;
            this._drugList.dataProvider = this._drugDP;
            return;
        }

        public showTabByIndex(arg1: int): void
        {
            this._tabbar.selectedIndex = arg1;
            this._tabbar.dispatchEvent(new MuiEvent(MuiEvent.GTABBAR_SELECTED_CHANGE));
            return;
        }

        public updateByDrugInfo(arg1: SDrugBagInfo): void
        {
            var loc2=0;
            var loc1=this._tabbar.selectedIndex;
            if (arg1) 
            {
                loc2 = arg1.remain;
                if (arg1.type == EDrug._EDrugLifeBag && loc1 == 0) 
                {
                    this._leftTxt.text = loc2.toString();
                }
                else if (arg1.type == EDrug._EDrugManaBag && loc1 == 1) 
                {
                    this._leftTxt.text = loc2.toString();
                }
                else if (arg1.type == EDrug._EDrugLifePetBag && loc1 == 2) 
                {
                    this._leftTxt.text = loc2.toString();
                }
            }
            return;
        }

        public /* override */ show(arg1: int=0, arg2: int=0): void
        {
            super.show(arg1, arg2);
            var loc1=this._tabbar.selectedIndex;
            if (loc1 != 0) 
            {
                if (loc1 != 1) 
                {
                    if (loc1 == 2) 
                    {
                        this.updateByDrugInfo(Cache.instance.pet.drugBagInfo);
                    }
                }
                else 
                {
                    this.updateByDrugInfo(Cache.instance.role.manaDrugBagInfo);
                }
            }
            else 
            {
                this.updateByDrugInfo(Cache.instance.role.lifeDrugBagInfo);
            }
            return;
        }

        /* internal  */serverBagDataChangehandler(arg1: Object=null): void
        {
            if (this.isHide == false) 
            {
                this.updateByIndex(this._tabbar.selectedIndex);
            }
            return;
        }

        /* internal */ /* var */_tabbar: GTabBar;

        /* internal */ /* var */_labelTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_leftTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_listSprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_drugList: TilePanel;

        /* internal */ /* var */_sellList: TilePanel;

        /* internal */ /* var */_drugDP: DataProvider;

        /* internal */ /* var */_sellDP: DataProvider;

        /* internal */ /* var */_tabData: Array<any>;
    }
